<?php
require_once("../resources/commons.php");
require_once("../data/User.php");
require_once("admin_commons.php");
checkAdminRights();

/**
 * Display the links for a modification of the account activity status
 * @param unknown_type $p_record
 */
function modifyStatus($p_record){
	if($p_record->getIsActive()){
		?>
		<a href="update_account.php?id=<?=base64_encode($p_record->getId()) ?>&updateType=activity">Unactivate</a>
		<?php
			if($p_record->getIsAdmin()){
		?>
		- <a href="update_account.php?id=<?=base64_encode($p_record->getId()) ?>&updateType=superadmin">Remove admin rights</a>
		<?php
			}else{
		?>
		- <a href="update_account.php?id=<?=base64_encode($p_record->getId()) ?>&updateType=superadmin">Grant admin rights</a>
		<?php
			}
			if($p_record->getIsFinanceAdmin()){
		?>
		- <a href="update_account.php?id=<?=base64_encode($p_record->getId()) ?>&updateType=financeadmin">Remove finance rights</a>
		<?php
			}else{
		?>
		- <a href="update_account.php?id=<?=base64_encode($p_record->getId()) ?>&updateType=financeadmin">Grant finance rights</a>
		<?php
			}
	}else{
		$l_credentials=$p_record->getCredentials();
		if(isset($l_credentials)){
		?>
		<a href="update_account.php?id=<?=base64_encode($p_record->getId()) ?>&updateType=activity">Re-activate</a>
		<?php
		}else{
			echo "Activation pending";
		}
	}
}

//If the specified parameter value is "true", the "un-activated" users are displayed in the table
define("DISPLAY_UNACTIVATED_REQUEST_PARAMETER","_f");
$isDisplayUnactivated=($_REQUEST[DISPLAY_UNACTIVATED_REQUEST_PARAMETER]=="1");

$tableData=new TableData("UserManagementTable");
if($isDisplayUnactivated){
	$records=User::getAll();
}else{
	$records=User::getFromCustomCriteria("WHERE isActive=:isActive",array("isActive" => true));
}
$tableData->setRecords($records);
$tableData->addHeader("firstName","First name");
$tableData->addHeader("lastName","Last name");
$tableData->addHeader("email","E-mail");
$tableData->addHeader("createdDate","Created");
$tableData->addAction("modifyStatus");

displayTop("User management",null,null);
?>
<p>Modify the status of the registered accounts.</p>
<?php
	if($isDisplayUnactivated){
?>
<p><a href="?<?=DISPLAY_UNACTIVATED_REQUEST_PARAMETER ?>=0">Filter unactivated accounts.</a></p>
<?php
	}else{
?>
<p><a href="?<?=DISPLAY_UNACTIVATED_REQUEST_PARAMETER ?>=1">Display the un-activated accounts.</a></p>
<?php
	}
?>
<?=displayTable($tableData) ?>
<?php
displayBottom();
?>
